package cn.sanch.basic.service.impl;

import cn.sanch.basic.mapper.BaseMapper;
import cn.sanch.basic.query.BaseQuery;
import cn.sanch.basic.service.IBaseService;
import cn.sanch.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sanch37
 * 2022/5/23
 */

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Transactional
    @Override
    public void save(T t) {
        baseMapper.save(t);
        // int i = 1/0;
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //查询总数量
        Long count = baseMapper.pageCount(baseQuery);
        //判断总数量是否为空
        if (count < 1) {
            return new PageList<>();
        }
        //查询当前页的分页数据
        List<T> ts = baseMapper.queryData(baseQuery);
        return new PageList<>(count, ts);
    }

    @Transactional
    @Override
    public void patchDelete(Long[] ids) {
        baseMapper.patchDelete(ids);
    }
}
